/**
 * 下载页面
 */
var _hmt = _hmt || [];
var urlPre = "http://"+window.location.host;
console.log("urlPre = "+urlPre);
var appUrl = "";
var appName = "";
var hasPrefix = false;
(function () {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?17eb9d706f6bdae49b8d821c67614494";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();

//loadVersionInfo(2);
$('body').click(function (e) {
  downloadApp();
});
var ua = navigator.userAgent + navigator.platform;
if (isIOS()) {
  loadVersionInfo(1);
}
if (isAndroid()) {
  loadVersionInfo(2);
}
function loadVersionInfo(type){
  var params = {"type":type};
  $.ajax({
    type: "post",
    url: "../downloadApp/getHighestVersion",
    data: params,
    dataType: "json",
    success: function(data) {
      console.info(data);
      if(data){
        appName = data.appName;
        appUrl = data.downloadPath;
        if(appUrl && appUrl.indexOf("http://oss.ztems.com") != -1){
        	hasPrefix = true;
        }
        console.log("appName = "+appName);
        console.log("appUrl = "+appUrl);
        console.log("hasPrefix = "+hasPrefix);
        $("#load-btn").val("下载" + appName + "APP");
      }
    },
    error : function(textStatus) {
        console.info("request error");
    }
  });
}
function isIOS() {//ios系统
  return /iphone|ipad|ipod/i.test(ua);
}
function isAndroid() {//android系统
  return /android|linux/i.test(ua);
}
function isWeixin() {//微信环境
  return /micromessenger/i.test(ua);
}

function downloadApp() {
  if (isWeixin()) {//微信环境   
      if (isIOS()) {
          var isDownloadIos = $(".downloadAppSafari").attr('class');
          if (isDownloadIos.indexOf('isdownload_ios') == 0) {
            $(".downloadAppSafari").removeClass('isdownload_ios');
          } else {
            $(".downloadAppSafari").addClass('isdownload_ios');
          }
          //window.location.href = "http://mp.weixin.qq.com/mp/redirect?url=" + "https://appsto.re/cn/K_ZH_.i";
          //window.location.href = "http://fusion.qq.com/cgi-bin/qzapps/unified_jump?appid=10551591&isTimeline=false&actionFlag=0&params=pname%3Dcom.zte.weidian%26versioncode%3D300%26channelid%3D%26actionflag%3D0&from=singlemessage&isappinstalled=0";
      }
      if (isAndroid()) {
          var isDownloadAndroid = $(".downloadAppSafari").attr('class');
          if (isDownloadAndroid.indexOf('isdownload_android') == 0) {
            $(".downloadAppSafari").removeClass('isdownload_android');
          } else {
            $(".downloadAppSafari").addClass('isdownload_android');
          }
          //window.location.href = "http://mp.weixin.qq.com/mp/redirect?url=" + "http://vpclub-img.oss-cn-shenzhen.aliyuncs.com/upload/100000056/201602/03/201602031538153281.apk";
          //window.location.href = "http://fusion.qq.com/cgi-bin/qzapps/unified_jump?appid=10551591&amp;isTimeline=false&amp;actionFlag=0&amp;params=pname%3Dcom.zte.weidian%26versioncode%3D300%26channelid%3D%26actionflag%3D0&amp;from=singlemessage&amp;isappinstalled=0";
      } else {
        alert('******不支持当前操作系统******' + navigator.platform);
      }
  } else {
      if (isIOS()) { // ios
        if (appUrl) {
          // window.location.href = appUrl;
          window.location.href = "https://itunes.apple.com/cn/app/yi-dong-da-zhang-gui/id1065828324?mt=8";
        } else {
          // window.location.href = "https://appsto.re/cn/K_ZH_.i";
          window.location.href = "https://itunes.apple.com/cn/app/yi-dong-da-zhang-gui/id1065828324?mt=8";
        }
      } else if (isAndroid()) { // android
        if (appUrl) {
        	if (hasPrefix) {
        		window.location.href = appUrl;
        	} else {
        		window.location.href = urlPre + appUrl;
        	}
        } else {
          window.location.href = urlPre + "/upload/100000056/201602/03/201602031538153281.apk";
        }
      } else {
        alert('******不支持当前操作系统******' + navigator.platform);
      }
  }
}
// 生成二维码
$('#qrcodeCanvas').qrcode({
  render: "canvas",
  text: urlPre + baseUrl + "/website/download_app.html",
  width: 220,
  height: 220
});
